package com.das.metrocloud.ssoserver.filter;

import org.springframework.stereotype.*;
import javax.ws.rs.ext.*;
import com.das.metrocloud.ssoserver.config.*;
import org.springframework.beans.factory.annotation.*;
import javax.ws.rs.container.*;
import com.das.metrocloud.ssoserver.util.*;
import org.jboss.resteasy.core.*;
import com.das.framework.common.utils.*;
import java.util.*;
import java.io.*;
import javax.ws.rs.core.*;
import com.das.framework.common.extvo.*;
import org.slf4j.*;
import org.springframework.util.*;

@Component
@Provider
public class AuthorizationRequestFilter implements ContainerRequestFilter
{
    protected static Logger log;
    private static PathMatcher matcher;
    @Value("${dascloud.authrization.enabled: true}")
    private Boolean authrizationEnabled;
    @Value("${dascloud.authrization.user.token-header:Authorization}")
    private String tokenName;
    @Autowired
    private JwtTokenSevice jwtTokenService;
    private static int i;
    
    public void filter(final ContainerRequestContext requestContext) throws IOException {
        requestContext.setProperty("start_time", (Object)System.currentTimeMillis());
        AuthorizationRequestFilter.log.info("jaxrs request filter\uff1a" + requestContext.getUriInfo().getAbsolutePath().toString());
        final String path = requestContext.getUriInfo().getPath();
        for (final String pattern : URLExcluder.urlList) {
            if (AuthorizationRequestFilter.matcher.match(pattern, path)) {
                return;
            }
        }
        if (!this.authrizationEnabled) {
            return;
        }
        final String jwtToken = requestContext.getHeaderString("JwtToken");
        final String authKey = requestContext.getHeaderString("AuthKey");
        if (jwtToken == null || "".equals(jwtToken.trim())) {
            this.stopAccess(requestContext, "401", "\u672a\u8ba4\u8bc1\u7684\u8bf7\u6c42!");
            return;
        }
        final LoginInfo user = new LoginInfo();
        user.setAccount("admin");
        user.setId("abc" + AuthorizationRequestFilter.i++);
        ResteasyContext.pushContext((Class)LoginInfo.class, (Object)user);
        ResteasyContext.pushContext((Class)TokenVo.class, (Object)null);
        ContextHandler.setLoginUser(user);
        try {
            final LoginInfo loginInfo = this.jwtTokenService.getInfoFromToken(jwtToken);
            ContextHandler.setLoginUser(loginInfo);
            ContextHandler.setJwtToken(jwtToken);
            ContextHandler.setAuthKey(jwtToken);
        }
        catch (Exception e) {
            AuthorizationRequestFilter.log.warn(e.getMessage(), (Throwable)e);
            this.stopAccess(requestContext, "401", "\u8bf7\u6c42\u62d2\u7edd\uff1a\u672a\u8ba4\u8bc1\u6216\u8ba4\u8bc1\u5931\u8d25!");
        }
    }
    
    private void stopAccess(final ContainerRequestContext requestContext, final String code, final String message) {
        requestContext.abortWith(Response.status(Response.Status.UNAUTHORIZED).entity((Object)new ResultVO(false, message, code)).build());
    }
    
    static {
        AuthorizationRequestFilter.log = LoggerFactory.getLogger((Class)AuthorizationRequestFilter.class);
        AuthorizationRequestFilter.matcher = (PathMatcher)new AntPathMatcher();
        AuthorizationRequestFilter.i = 0;
    }
}
